﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemBUS;

namespace NangLuongTieuDiemGUI.NangLuongTieuDiemUserControl
{
    public partial class ucGoiEmail : UserControl
    {
        #region "[khai báo biến toàn cục]"
        public DelegateShowItem CloseItem = null;
        #endregion

        public ucGoiEmail()
        {
            InitializeComponent();
        }

        #region "[thoát]"
        private void btnThoat_Click(object sender, EventArgs e)
        {
            try {
                if (CloseItem != null)
                {
                    CloseItem(false);
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnThoat_Click", ex);
            }
        }
        #endregion

        #region "[btnGoiEmail_Click]"// Cao Hoang An - 1241301
        /// <summary>
        /// btnGoiEmail_Click
        /// </summary>
        private void btnGoiEmail_Click(object sender, EventArgs e)
        {
            try
            {
                bool isSend = true;
                lbIsNguoiGoi.Visible = lbIsMatKhau.Visible = lbIsNguoiNhan.Visible = lbIsTieuDe.Visible = lbIsNoiDung.Visible = false;
                if (txtEmailNguoiGoi.Text == "")
                {
                    lbIsNguoiGoi.Visible = true;
                    isSend = false;
                }
                if (txtMatKhau.Text == "")
                {
                    lbIsMatKhau.Visible = true;
                    isSend = false;
                }
                if (txtEmailHoTro.Text == "")
                {
                    lbIsNguoiNhan.Visible = true;
                    isSend = false;
                }
                if (txtTieuDe.Text == "")
                {
                    lbIsTieuDe.Visible = true;
                    isSend = false;
                }
                if (txtNoiDung.Text == "")
                {
                    lbIsNoiDung.Visible = true;
                    isSend = false;
                }
                if (isSend == true)
                {
                    if (SentEmail(txtEmailNguoiGoi.Text + "@" + cbMail.Text, txtMatKhau.Text, txtTieuDe.Text, txtNoiDung.Text, txtEmailHoTro.Text, cbMail.SelectedIndex))
                    {
                        MessageBox.Show("Đã gửi mail thành công", "Thông báo:");
                    }
                    else
                    {
                        MessageBox.Show("Chưa gửi được email, có thể do sai email người gửi hoặc mật khẩu", "Thông báo:");
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("btnGoiEmail_Click", ex);
            }
        }
        #endregion

        #region "[SentEmail]"// Cao Hoang An - 1241301
        /// <summary>
        /// SentEmail
        /// </summary>
        protected bool SentEmail(string nguoiGoi,string matKhau, string tieuDe, string noiDung, string nguoiNhan, int flag)
        {
            try
            {
                string strSmtp = "";
                bool SSl = false;
                switch (flag)
                {
                    case 0:
                        strSmtp = "smtp.mail.yahoo.com";
                        break;
                    case 1:
                        strSmtp = " smtp.gmail.com";
                        SSl = true;
                        break;
                }
                using (var client = new SmtpClient(strSmtp, 25))
                {
                    
                    client.Credentials = new NetworkCredential(nguoiGoi, matKhau);
                    client.EnableSsl = SSl;
                    var mail = new MailMessage();
                    mail.From = new MailAddress(nguoiGoi, "Năng Lượng Tiêu Điểm");
                    mail.To.Add(nguoiNhan);
                    mail.Subject = tieuDe;
                    mail.Body = noiDung;
                    client.Send(mail);
                }
            }
            catch(Exception ex)
            {
                InSert_LoiHeThong("SentEmail", ex);
                return false;
            }
            return true;
        }
        #endregion

        #region "[txtMatKhau_MouseDown]"// Cao Hoang An - 1241301
        /// <summary>
        /// txtMatKhau_MouseDown
        /// </summary>
        private void txtMatKhau_MouseDown(object sender, MouseEventArgs e)
        {
            txtMatKhau.Text = "";
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "ucGoiEmail_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            NangLuongTieuDiem_Management.LoiHeThong_Insert(lhtDto);
            MessageBox.Show(lhtDto.TenLoi);
        }
        #endregion 
    }
}
